package com.auto.survey.activity.monitoring.station

import android.content.Intent
import android.graphics.Color
import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.auto.survey.R
import com.auto.survey.activity.monitoring.InitCollectionActivity
import com.auto.survey.activity.monitoring.IntentKey.KEY_INTENT_OBJECT_ID
import com.auto.survey.activity.monitoring.IntentKey.KEY_INTENT_PROJET_ID
import com.auto.survey.activity.monitoring.IntentKey.KEY_INTENT_STATION_ID
import com.auto.survey.activity.monitoring.adapter.MontoringStationAdapter
import com.auto.survey.activity.monitoring.dao.MonitoringRepository
import com.auto.survey.activity.monitoring.entity.MonitoringStation
import com.auto.survey.base.BaseActivity
import com.auto.survey.util.Helper
import com.auto.survey.widget.CommonTitle.Companion.TEXT_POSITON_RIGHT
import kotlinx.coroutines.launch

/**
 * 监测数据采集页面
 */
class MonitoringCollectionActivity :BaseActivity(){

    private var projectId:Int = -1
    private var objectId:Int = -1

    private val database = MonitoringRepository()
    private val adapter = MontoringStationAdapter()
    override fun initView() {

        projectId = intent.getIntExtra(KEY_INTENT_PROJET_ID, -1)
        objectId = intent.getIntExtra(KEY_INTENT_OBJECT_ID, -1)

        mTopBar?.visibility = View.VISIBLE
        mTopBar?.setBackgroundResource(R.color.action_bar)

        mComment_title?.visibility = View.VISIBLE
        mComment_title?.setBackgroundResource(R.color.action_bar)
        mComment_title?.setTitle("监测数据采集")

        mComment_title?.setRightTitle("新建测站")
        mComment_title?.setTitleColor(Color.WHITE, TEXT_POSITON_RIGHT)
        mComment_title?.setOnRightTextClickListener(Helper.debounce{
            val intent = Intent()
            intent.putExtra(KEY_INTENT_PROJET_ID, projectId)
            intent.putExtra(KEY_INTENT_OBJECT_ID, objectId)
            intent.setClass(this@MonitoringCollectionActivity, CreateStationActivity::class.java)
            startActivity(intent)
        })

        val rv = findViewById<RecyclerView>(R.id.rv_monitoring_collection)
        rv.layoutManager = LinearLayoutManager(this)
        rv.adapter = adapter
        adapter.setOnItemClick(object: MontoringStationAdapter.OnItemClickListener{
            override fun onItemClick(station: MonitoringStation) {

                val intent = Intent()
                intent.putExtra(KEY_INTENT_PROJET_ID, projectId)
                intent.putExtra(KEY_INTENT_OBJECT_ID, objectId)
                intent.putExtra(KEY_INTENT_STATION_ID, station.id)
                intent.setClass(this@MonitoringCollectionActivity, InitCollectionActivity::class.java)
                startActivity(intent)
            }

            override fun onItemDetailClick(station: MonitoringStation) {
                val intent = Intent()
                intent.putExtra(KEY_INTENT_STATION_ID, station.id)
                intent.setClass(this@MonitoringCollectionActivity, DetailStationActivity::class.java)
                startActivity(intent)
            }
        })
    }

    override fun onResume() {
        super.onResume()
        lifecycleScope.launch {
            database.getStationByObjectId(objectId).collect{
                adapter.setItems(it)
            }
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_monitoring_collection
    }
}